package com.ruoyi.quartz.thread;

import com.ruoyi.bizsys.domain.ExtMonnifyReservedAccounts;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.ExtMonnifyReservedAccountsMapper;
import com.ruoyi.bizsys.service.IExtMonnifyReservedAccountsService;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.IBRepaymentService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class TransferToUsThread implements Callable {
    private String threadName;
    private String redisName;
    private RedisCache redisCache;

    private IBRepaymentService iBRepaymentService;
    private IRepaymentOrderService iRepaymentOrderService;

    private IExtMonnifyReservedAccountsService extMonnifyReservedAccountsService;

    public TransferToUsThread(String threadName, String redisName,
                          IBRepaymentService iBRepaymentService, RedisCache redisCache
            ,IRepaymentOrderService iRepaymentOrderService
            ,IExtMonnifyReservedAccountsService extMonnifyReservedAccountsService) {
        super();
        this.threadName = threadName;
        this.redisName = redisName;
        this.redisCache = redisCache;
        this.iBRepaymentService = iBRepaymentService;
        this.iRepaymentOrderService = iRepaymentOrderService;
        this.extMonnifyReservedAccountsService = extMonnifyReservedAccountsService;
    }

    @Override
    public String toString() {
        return "BatchJobThread [threadName=" + threadName+ ", redisName=" + redisName + "]";
    }

    @Override
    public Object call() throws Exception {
        String result = ":success";
        log.info(this.threadName+"=======[START]===" + this.toString());

        List<RepaymentOrder> list = redisCache.getCacheList(this.redisName);
        log.info("共有{}个代扣交易流水号需要执行", list.size());

        for (RepaymentOrder order : list) {
            log.info("代扣交易流水号{}开始执行跑批操作", order.getCustNo());

            // extMonnifyReservedAccountsService accountReference已经不是custno 了，要去查询改表进行多个查询获取多个数据
            ExtMonnifyReservedAccounts extMonnifyReservedAccounts = new ExtMonnifyReservedAccounts();
            extMonnifyReservedAccounts.setState(BizConstants.BIZ_STATUS_1);
            extMonnifyReservedAccounts.setCustNo(order.getCustNo());
            List<ExtMonnifyReservedAccounts> listExtMonnifyReservedAccount =
                    extMonnifyReservedAccountsService.selectExtMonnifyReservedAccountsList(extMonnifyReservedAccounts);
            if(!Collections.isEmpty(listExtMonnifyReservedAccount)){
                log.info("listExtMonnifyReservedAccount:"+listExtMonnifyReservedAccount.size());
                doExtOrder(order,listExtMonnifyReservedAccount);
            }else{
                log.info("代扣交易流水号{}执行跑批操作正常结束,ExtMonnifyReservedAccounts:数据为空", order.getPayTransSerialNo());
            }
        }

        redisCache.deleteObject(this.redisName);

        log.info(this.threadName+"=======[END]===run result:" + result);
        return threadName + result;
    }

    private void doExtOrder(RepaymentOrder order,List<ExtMonnifyReservedAccounts> listExtMonnifyReservedAccount) {

        for (int i = 0; i < listExtMonnifyReservedAccount.size(); i++) {
            try{
                boolean allReservedAccountOrder =
                        iBRepaymentService.getAllReservedAccountOrder(listExtMonnifyReservedAccount.get(i).getCustNo());
                if(allReservedAccountOrder){
                    RepaymentOrder repaymentOrder = new RepaymentOrder();
                    repaymentOrder.setRepaymentNo(order.getRepaymentNo());
                    repaymentOrder.setRepayStatus(1);
                    List<RepaymentOrder> repaymentOrders = iRepaymentOrderService.selectRepaymentOrderList(repaymentOrder);
                    if(repaymentOrders==null || repaymentOrders.size()==0){
                        //没找到   并且还款订单不为1（防止并发）   状态改为10
                        iBRepaymentService.closeByRepayemntNo(order.getRepaymentNo());
                    }
                }
            }catch (Exception e){
                log.error("代扣交易流水号{}执行跑批操作异常结束", order.getPayTransSerialNo());
                return;
            }

            log.info("代扣交易流水号{}执行跑批操作正常结束", order.getPayTransSerialNo());
        }
    }

}
